package com.springboot.repository;

        import com.springboot.domain.BtDemand;
        import com.springboot.domain.BtTask;
        import com.springboot.dto.SuRecordDTO;
        import com.springboot.dto.TaskDemandDTO;
        import com.springboot.dto.TaskDetailDTO;
        import org.springframework.data.domain.Page;
        import org.springframework.data.domain.Pageable;
        import org.springframework.data.jpa.repository.JpaRepository;
        import org.springframework.data.jpa.repository.Modifying;
        import org.springframework.data.jpa.repository.Query;
        import org.springframework.stereotype.Repository;

        import java.util.List;

@Repository
public interface TaskRepository extends JpaRepository<BtTask, String> {

    @Query(value = "select new com.springboot.dto.TaskDemandDTO (bt.id,bt.status,bt.annoRead,bt.receRead," +
            "bt.createTime,bd.name,bd.type,bd.description,bu.userName,bu1.userName) from BtTask bt left join bt.demand bd,BtUser bu,BtUser bu1  where bu.userId = bt.receiver and bu1.userId = bt.announcer and bt.announcer = ?1",
            countQuery = "select count(bt.id) from BtTask bt left join bt.demand bd,BtUser bu,BtUser bu1  where bu.userId = bt.receiver and bu1.userId = bt.announcer and  bt.announcer = ?1")
    Page<TaskDemandDTO> findByAnnouncer(String bankId, Pageable pageable);

    @Query(value = "select new com.springboot.dto.TaskDemandDTO (bt.id,bt.status,bt.annoRead,bt.receRead," +
            "bt.createTime,bd.name,bd.type,bd.description,bu.userName,bu1.userName) from BtTask bt left join bt.demand bd,BtUser bu,BtUser bu1  where bu.userId = bt.receiver and bu1.userId = bt.announcer and bt.receiver = ?1",
            countQuery = "select count(bt.id) from BtTask bt left join bt.demand bd,BtUser bu,BtUser bu1  where bu.userId = bt.receiver and bu1.userId = bt.announcer and  bt.receiver = ?1")
    Page<TaskDemandDTO> findByReceiver(String receiver, Pageable pageable);

    @Query(value = "select new com.springboot.dto.TaskDemandDTO (bt.id,bt.status,bt.annoRead,bt.receRead," +
            "bt.createTime,bd.name,bd.type,bd.description,bu.userName,bu1.userName) from BtTask bt left join bt.demand bd,BtUser bu,BtUser bu1 " +
            " where bu.userId = bt.receiver and bu1.userId = bt.announcer and bd.platShown= '1'",
            countQuery = "select count(bt.id) from BtTask bt left join bt.demand bd,BtUser bu,BtUser bu1 where bu.userId = bt.receiver and bu1.userId = bt.announcer and bd.platShown= '1'")
    Page<TaskDemandDTO> findByAll(Pageable pageable);

    @Query("select distinct new com.springboot.dto.TaskDetailDTO(bt.id,b.id,b.name,b.type,b.projectSize,b.description,b.money,b.deadline,b.tecQualification,b.busQualification,b.specReq,b.difficulty,bt.createTime)" +
            " from BtTask bt left join bt.demand b where bt.id = ?1")
    TaskDetailDTO findTaskById(String id);

    @Modifying
    @Query("update BtTask bt set bt.annoRead = ?2 where bt.id = ?1")
    void updateAnnoRead(String id, Integer read);

    @Modifying
    @Query("update BtTask bt set bt.receRead = ?2 where bt.id = ?1")
    void updateReceRead(String id, Integer read);

    @Modifying
    @Query("update BtTask bt set bt.status = ?2 where bt.demand = ?1 and bt.status <> 2")
    void closeByDemand(BtDemand demand, Integer close);

    @Modifying
    @Query("update BtTask bt set bt.status = ?1 where bt.id = ?2")
    void updateStatus(Integer status, String id);

    @Query("select count(bt.id) from BtTask bt where bt.annoRead=0 and bt.announcer =?1 ")
    Integer findUnReadByAnnouncer(String announcerId);

    @Query("select count(bt.id) from BtTask bt where bt.receRead=0 and bt.receiver =?1")
    Integer findUnReadByReceiver(String receiverId);

    @Query("select new com.springboot.dto.SuRecordDTO(bu.userName,bu1.userName,bt.createTime,bd.money,bd.name) from BtTask bt left join " +
            "bt.demand bd,BtUser bu,BtUser bu1 where bu.userId = bt.receiver and bu1.userId = bt.announcer  and bt.announcer=?1 and bt.status=2 order by bt.createTime desc")
    List<SuRecordDTO> findBankSuRecord(String bankId);

    @Query("select new com.springboot.dto.SuRecordDTO(bu.userName,bu1.userName,bt.createTime,bd.money,bd.name) from BtTask bt left join " +
            "bt.demand bd,BtUser bu,BtUser bu1 where bu.userId = bt.receiver and bu1.userId = bt.announcer  and bt.receiver=?1 and bt.status=2 order by bt.createTime desc")
    List<SuRecordDTO> findUserSuRecord(String userId);

    @Query("select bt from BtTask bt where bt.status=2 and bt.demand =?1")
    BtTask findByDemandId(BtDemand btDemand);
}